import axios from "axios"
const data = {

    actions: {

        async getTreeList({ commit }) {
            const result = await axios.get('/mock/treeData')
            commit('GET_TREE_DATA', result.data.data)
        },
        async savetreeLISt({ commit,dispatch,state }, val) {
            console.log(val, 'val1')
            const result = await axios.get('/mock/treeData') //等响应成功，失败已经同意处理了
            function addExtraObject(data, title, extraObject) {
                // 遍历每个节点
                for (let node of data) {
                    // 如果节点的title符合条件
                    if (node.title === title) {
                        // 添加额外的对象
                        node.extra = extraObject;
                    }
                    // 如果节点有子节点
                    if (node.children && node.children.length > 0) {
                        // 递归调用在子节点中查找并添加对象
                        addExtraObject(node.children, title, extraObject);
                    }
                }
                // 返回修改后的树形结构数组
                return data;
            }
            const res = addExtraObject(state.treeData, val.title, val)
            console.log(res, 'va1111111111112l')
            // }
            commit('GET_SAVETREElIST', res)
        },

        async savetreeLIStTwo({ commit,dispatch,state }, val) {
            const result = await axios.get('/mock/treeData') //等响应成功，失败已经同意处理了
            function addExtraObject(data, title, extraObject) {
                // 遍历每个节点
                for (let node of data) {
                    // 如果节点的title符合条件
                    if (node.title === title) {
                        // 添加额外的对象
                        node.extra = extraObject;
                    }
                    // 如果节点有子节点
                    if (node.children && node.children.length > 0) {
                        // 递归调用在子节点中查找并添加对象
                        addExtraObject(node.children, title, extraObject);
                    }
                }
                // 返回修改后的树形结构数组
                return data;
            }
            const res = addExtraObject(state.treeData, val.title, val)
            console.log(res, 'va22222222222222l')
            // console.log(res,'resVUEXzhong de shuvj v')
            commit('GET_MODELDATAVALUE', res)
        },
    },


    mutations: {
        GET_TREE_DATA: (state, treeInfo) => {
            console.log(treeInfo, '//vuex mutations')
            state.treeData = treeInfo
        },
        GET_SAVETREElIST: (state, tre) => {
            state.treeData = tre
            console.log("treeData",tre)
        },
        GET_MODELDATAVALUE: (state, tre) => {
            state.treeData = tre
            console.log("treeData2",state.treeData)
        }
    },

    //定义一个state（用于存储数据）
    state: {
        treeData: [],

    }

}

export default data

